<?php

/*
  company:tiesh.com
  project:common
  function: 记录用户操作

  created by: Bach
  created date: 2017-02-15
  description:

  modification tp5 1.00
  modified by:
  description:
 */

namespace app\common\model;
use think\Model;
class Action extends Model {
    private $userId;
    public function __construct()
    {
        if(!$this->userId){
            $user = new User;
            $this->userId = $user->getUserId();
        }
    }

    static function add($action, $target = null, $content = null, $amount = null,$id = null) {
        $user = new User;
        $userid = $user->getUserId();
        $sqlData = array(
            'MYSQL_USER' => config('database.username'),
            'USER_ID'   => $userid,
            'ACTION'    => $action,
            'TARGET'    => $target,
            'CONTENT'   => $content,
            'AMOUNT'    => $amount,
            'TIME'      => time(),
            'IP'        => getIp(),
            "id"        =>$id
        );
        $http = "HTTP_HOST,HTTP_USER_AGENT,HTTP_ACCEPT_LANGUAGE,PATH_INFO";
        foreach (explode(",",$http) as $key) {
            $sqlData[$key] = $_SERVER[$key];
        }
        $sqlData = array_change_key_case($sqlData,CASE_LOWER);
        $sql = db('action')->insert($sqlData);
    }
    //查找
    public function getlist($data)
    {
        if ($data['start'] && $data['end']) {
            $startime = strtotime($data['start']);
            $endtime = strtotime($data['end']);
        }
        //时间区间判断
        if (isset($startime) && isset($endtime) && $startime && $endtime) {
            if ($startime == $endtime) {
                $endtime = $startime + 86400;
                $where = "create_time > $startime and create_time < $endtime";
            } else {
                $endtime = $endtime + 86399;
                $where = "create_time > $startime and create_time < $endtime";
            }
        }else{
            $startime = strtotime('-1 week')+86400;
            $endtime = strtotime("next Monday")-1;
            $where = "create_time > $startime and create_time < $endtime";
        }
//        header("Content-Disposition:attachment;filename=" . $filename . ".csv"); //告诉浏览器输出的文件名
        if($data['select']){
            $where .= " and http_host = $_SERVER[HTTP_HOST] and path_info like '%$data[select]%'";
        }
        $list = db('action')->field('id,user_id,action,target,content,time')->where($where)->order('create_time')->select();
        //查询板块名
        foreach ($list as $k =>$v){
            if($v['id']){
                $list[$k]['name'] = db('mall')->where("mall_id = $v[id]")->find()['name'];
            }
            $user_info = model('user')->getInfoById($v['user_id']);
            $list[$k]['user_name'] = isset($user_info['set_name']) ? $user_info['set_name'] : $user_info['user_name'];
        }
        if(!$list) return ['state'=>1,'data'=>[]];
        return ['state'=>1,'data'=>['list'=>$list]];

    }

    //下载
    public function download($data)
    {
        $output = fopen('php://output', 'w') or die("can't open php://output");
//        header("Content-Type: application/csv;charset:UTF-8");
        if(!$this->userId)return ['state'=>0,'errormsg'=>'请登录'];

        $name = "hongyanche_" . "forum";
        if ($data['start'] && $data['end']) {
            $startime = strtotime($data['start']);
            $endtime = strtotime($data['end']);
        }
        //时间区间判断
        if (isset($startime) && isset($endtime) && $startime && $endtime) {
            if ($startime == $endtime) {
                $endtime = $startime + 86400;
                $filename = $name . "_downdload" . " '" . $data['start'] . "' " . date('Y-m-d H:i:s', time());
                $where = "create_time > $startime and create_time < $endtime";
            } else {
                $filename = $name . "_downdload" . " '" . $data['start'] . '--' . $data['end'] . "' " . date('Y-m-d H:i:s', time());
                $endtime = $endtime + 86399;
                $where = "create_time > $startime and create_time < $endtime";
            }
        }else{
            $startime = strtotime('-1 week')+86400;
            $endtime = strtotime("next Monday")-1;
            $filename = $name . "_downdload" . " '" . date('Y-m-d',$startime) . '--' . date('Y-m-d',$endtime) . "' " . date('Y-m-d H:i:s', time());
            $where = "create_time > $startime and create_time < $endtime";
        }
//        header("Content-Disposition:attachment;filename=" . $filename . ".csv"); //告诉浏览器输出的文件名
        if($data['select']){
            $where .= " and http_host = $_SERVER[HTTP_HOST] and path_info like '%$data[select]%'";
        }
        $list = db('action')->field('id,user_id,action,target,content,time')->where($where)->order('create_time')->select();
        //查询板块名
        foreach ($list as $k =>$v){
            if($v['id']){
                $list[$k]['name'] = db('mall')->where("mall_id = $v[id]")->find()['name'];
            }
            $user_info = model('user')->getInfoById($v['user_id']);
            $list[$k]['user_name'] = isset($user_info['set_name']) ? $user_info['set_name'] : $user_info['user_name'];
        }
        if ($data['code'] == 'utf-8') {
            $create_time = "创建时间";
            $action = "操作行为";
            $user_name = "操作人";
            $name = "名称";
            $target = "行为目标";
            $content = "行为内容";
        } else {
            $create_time = iconv("UTF-8", "GBK//IGNORE", "创建时间");
            $action = iconv("UTF-8", "GBK//IGNORE", "操作行为");
            $user_name = iconv("UTF-8", "GBK//IGNORE", "操作人");
            $name = iconv("UTF-8", "GBK//IGNORE", "商品名");
            $target = iconv("UTF-8", "GBK//IGNORE", "行为目标");
            $content = iconv("UTF-8", "GBK//IGNORE", "行为内容");
        }
        $table_head = [$create_time,$name,$action,$target,$content,$user_name];
        fputcsv($output, $table_head);
        $forumlist = [];
        foreach ($list as $k =>$v){
            if ($data['code'] == 'utf-8') {
                $forumlist[$k][$create_time] = date('Y-m-d H:i:s', $v['time']);
                $forumlist[$k][$name] = $v['name'];
                $forumlist[$k][$action] = $v['action'];
                $forumlist[$k][$target] = $v['target'];
                $forumlist[$k][$content] = $v['content'];
                $forumlist[$k][$user_name] = $v['user_name'];
            }else{
                $forumlist[$k][$create_time] = date('Y-m-d H:i:s', $v['time']);
                $forumlist[$k][$name] = iconv("UTF-8", "GBK//IGNORE", $v['name']);
                $forumlist[$k][$action] = iconv("UTF-8", "GBK//IGNORE", $v['action']);
                $forumlist[$k][$target] = iconv("UTF-8", "GBK//IGNORE", $v['target']);
                $forumlist[$k][$content] = iconv("UTF-8", "GBK//IGNORE", $v['content']);
                $forumlist[$k][$user_name] = iconv("UTF-8", "GBK//IGNORE", $v['user_name']);
            }
        }
        foreach ($forumlist as $e) {
            fputcsv($output, array_values($e));
        }

        fclose($output) or die("can't close php://output");
        exit;
    }
}
